<?php
			
	/* SVN FILE: $Id: report-json.php 19 2011-03-15 13:12:33Z michele.andreoletti@gmail.com $ */
	
	/**
	* Project Name : arcadia
	*
	* @author $Author: michele.andreoletti@gmail.com $
	* @version $Revision: 19 $
	* @lastrevision $Date: 2011-03-15 13:12:33 +0000 (Tue, 15 Mar 2011) $
	* @filesource $URL: http://arcadia.googlecode.com/svn/trunk/budget/report-json.php $
	*/

	require_once 'include/functions.php';
	require_once 'include/const.inc.php';
	require_once '../include/auth.inc.php';
	require_once '../include/conn.inc.php';
	
	$sScript = fnCurrentScript();
	
	$sName = fnGetVal('sName');
		
	if (!isset($sWhere) || trim($sWhere)=='') { $sWhere = ' 1 '; }
	
	fnEvaluateTablesAccessLvl($aTABLE_SECURITY_CHECK, true);
	
	// $_GET['sTable'] // Viene definita qual è la tabella da visualizzare
	
	$sTable = fnGetTableVal('sTable', fnTableDefault($sTABLE_DEFAULT, $aTABLE), '', fnArray2Check($aTABLE), false); 
	
	// $sTableDefault mi serve perchè viene usato successivamente dalla funzione fnSettingsArray()
	$sTableDefault = fnTableDefault($sTABLE_DEFAULT, $aTABLE);

	fnTableConst($sTable);
	fnViewConst($sViewItemLink);
	 
	/**
	 * $iAccessLvl	
	 *				
	 *				Se e' definito un valore di $iAccessLvl specifico per la tabella
	 *				viene utilizzato come $iAccessLvl, altrimenti viene utilizzato il valore di 
	 *				$iAccessLvl definito per l'applicazione
	 *
	 */
	 
	$iAccessLvl = (!is_null(fnGetAccessLvl('table', $sTable)) ? fnGetAccessLvl('table', $sTable) : fnGetAccessLvl('app'));

	//
	// Inizializzo la variabile $sWhere
	//

	if (trim($sTableSelectWhere) != '1') { $sWhere .= " AND ".$sTableSelectWhere; }
	// l'array $aWhere è stato inizializzata dalla funzione fnEvaluateTablesAccessLvl()
	if (isset($aWhere[$sTableSelectFrom]['sTableSelectWhere'])) { $sWhere .= $aWhere[$sTableSelectFrom]['sTableSelectWhere']; }
	
	//
	// completamento della variable $sWhere
	//
		
	// $aSelect
	$aSelect = fnSettingsArray($sTableDefault, 'select');
	foreach ($aSelect as $aFilter) {
		$$aFilter['sField'] = fnReadSessionValue($aFilter['sField'], 'main-table', $sAPP);
		if (!empty($$aFilter['sField'])) {
			$sWhere .= fnSelectFilter('sWhere', $aFilter, $sTable);
		}
	}
			
	// $bTagsShow
	if (isset($bTagsShow) && $bTagsShow) { 
		$sTag = fnGetTableVal('sTag', '', '', array(), false);
		$sWhere .= fnTagsFilter('sWhere', $sTag, $sTable, $sAPP);
	}
	
	// die($sWhere); // DEBUG
	
	// $aFieldsDataType
	fnFieldsDataTypeArray($sTableSelectFrom);

	/* Ordering */
	if ( isset( $_GET['iSortCol_0'] ) ) {
		$sOrder = "ORDER BY  ".$aREPORT[$sName]['aColumns'][$_GET['iSortCol_0']];
	}
	
		$sQuery = "
			SELECT SQL_CALC_FOUND_ROWS * 
			FROM $sTableSelectFrom
			WHERE $sWhere
			$sOrder
		";	

		$rResult = custom_mysql_query( $sQuery);
		
		$sQuery = "
			SELECT FOUND_ROWS()
		";
		$rResultFilterTotal = custom_mysql_query( $sQuery);
		$aResultFilterTotal = mysql_fetch_array($rResultFilterTotal);
		$iFilteredTotal = $aResultFilterTotal[0];
		
		$sQuery = "
			SELECT COUNT(id)
			FROM $sTableSelectFrom WHERE $sTableSelectWhere
		";
		$rResultTotal = custom_mysql_query( $sQuery);
		$aResultTotal = mysql_fetch_array($rResultTotal);
		$iTotal = $aResultTotal[0];
		
		$sOutput = '{';
		$sOutput .= '"sEcho": '.intval($_GET['sEcho']).', ';
		$sOutput .= '"iTotalRecords": '.$iTotal.', ';
		$sOutput .= '"iTotalDisplayRecords": '.$iFilteredTotal.', ';
		$sOutput .= '"aaData": [ ';
		
		while ($aRow = mysql_fetch_array($rResult)) {
			
			$sOutput .= "[";

			foreach ($aREPORT[$sName]['aColumns'] as $sField)  {
				
				$sValue = ($sField=='iShowTags' ? '' : preg_replace('/[\n\r]/','<br/>',$aRow[$sField]));
					
				if (fnCheckFieldsDataType($sField, 'date')) {
					$bExpired  = (strtotime($aRow[$sField])-strtotime('now') < 0 ? true : false); // expired
					$sValue = date_convert($sValue, 'html');
					if (isset($aExpiredValueHighlight) && in_array($sField, $aExpiredValueHighlight) && $sValue != '&nbsp;') {
						$sValue = "<span class='".($bExpired ? 'expired-value' : 'not-expired-value')."'>$sValue</span>";
					
					}
				}
				// conversione formato numerico con separatore migliaia
				else if (fnCheckFieldsDataType($sField, 'currency')) {
					if (empty($aRow[$sField])) {
						$sValue = '';
					
					} else {
						$bNegative  = ($sValue < 0 ? true : false); // negative
						$sValue = number_format($sValue, (intval($sValue) != $sValue ? 2 : 0), ',', '.');
						if (isset($aNegativeValueHighlight) && in_array($sField, $aNegativeValueHighlight)) {
							$sValue = "<span class='".($bNegative ? 'negative-value' : 'positive-value')."'>".$sValue."</span>";
						}
					
					}
				}
							
				else if ($sField == "iShowTags") {
					$sValue =  "<div class='tags-show' id='tags-show-".$aRow['id']."'>".fnTagsView('view', $aRow['id'], '', $sTable, '', $sAPP, true, false, false)."</div>";
				}
				
				if (empty($sValue)) $sValue = '&nbsp;';
				
				$sOutput .= "\"".preg_replace("/\"/", "\\\"", $sValue)."\", ";
			}
			$sOutput = substr_replace($sOutput, '', -2);
			$sOutput .= "],";
	
		}
		$sOutput = substr_replace( $sOutput, "", -1 );
		$sOutput .= '] }';
		
		echo $sOutput;

?>